package hanelsoft.vn.dictionaylearn.control.service;

import hanelsoft.vn.dictionaylearn.R;

import java.io.IOException;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.widget.RemoteViews;

public class WidgetMyWidgetIntentReceiver extends BroadcastReceiver {

	String wordWidget="", contentWidget="";
	SharedPreferences shareWidget;
	MediaPlayer mp;
	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals("hanelsoft.vn.dictionaylearn.control.service.WIDGET_LEARN")){
			updateWidget(context);
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vocabulary_widget);
			ComponentName myWidget = new ComponentName(context, WidgetMyWidgetIntentReceiver.class);
		    AppWidgetManager manager = AppWidgetManager.getInstance(context);
		    manager.updateAppWidget(myWidget, remoteViews);	
		}
	}
	private void updateWidget(Context context) {
		shareWidget = context.getSharedPreferences("ShareWidget", 0);
		wordWidget = shareWidget.getString("contentWidget", "");
		contentWidget = shareWidget.getString("wordWidget", "");
		String fullPathFilePro = shareWidget.getString("fullPathFilePro", "");
		mp = new MediaPlayer();
		try {
			mp.setDataSource(fullPathFilePro);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mp.setLooping(false);
		mp.setVolume(1.0f, 1.0f);
		if(mp.isPlaying() == false)
		{
			mp.start();
		}
		else
		{
			mp.stop();
			mp.start();
		}
	}
}
